import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		cartlist: []
	},
	// actions: {
	// 	joinCart(ctx, detailList) {
	// 		console.log(detailList.title)
	// 	}
	// },
	mutations: {
		joinCart(state, detailList) {
			// 判断购物车内是否已经含有该条商品
			if(state.cartlist.some(item => {
				return item.name == detailList.title
			})){
				//检测数组中已有的元素，并修改已有元素的数量加一
				let itemHave = state.cartlist.filter(item => item.name == detailList.title)
				let itemHaveid = (state.cartlist.indexOf(itemHave[0]))
				state.cartlist[itemHaveid].num += 1
			}else{
				//如果购物车内没有该元素则将该元素加入购物车内
				state.cartlist.push(
					{"num":1, "name": detailList.title, "price": detailList.price, "imgUrl": detailList.imgUrl, "flag": false}
				)
			}
			uni.showToast({
				title: '加入成功',
				duration: 1000
			})
		}
	}
})

export default store